package com.i7time.qutan.controller;

import com.alibaba.dubbo.rpc.RpcException;
import com.i7time.entity.FriendInfo;
import com.i7time.entity.User;
import com.i7time.qutan.utils.IMUtil;
import com.i7time.qutan.utils.ReturnMap;
import com.i7time.service.DynamicService;
import com.i7time.service.FriendService;
import com.i7time.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Hsia on 16/6/21.
 */
@Controller
@RequestMapping("/friend")
public class FriendController {
    private Logger logger=Logger.getLogger(this.getClass());

    @Autowired
    FriendService friendService;
    @Autowired
    UserService userService;
    @Autowired
    DynamicService dynamicService;


    /**
     *  好友详情
     * @return
     */
    @ResponseBody
    @RequestMapping("detail")
    public Map detailFriend(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();
            long userid=Long.valueOf((String)map.get("userid"));
            long friendid=Long.valueOf((String)map.get("friendid"));

            FriendInfo friendInfo=friendService.getFriendById(userid,friendid);
            if(null==friendInfo){
                result.put("msg","not_friend");
                return result;
            }
            HashMap friendDetail=new HashMap<String,Object>();
            friendDetail.put("userid",friendid);
            friendDetail.put("nickname",friendInfo.getFriendNickName());
            friendDetail.put("avatar",friendInfo.getFriendAvatar());
            List<String> dynamic=dynamicService.getRecentPhotos(friendid+"");
            friendDetail.put("dynamicPhotos",dynamic);



            result.put("friendDetail",friendDetail);
            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }




    /**
     *  列出好友
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    public Map listFriend(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();

            long userid=Long.valueOf((String)map.get("userid"));

            List<FriendInfo> friendlist=friendService.getFriendList(userid);
            List<HashMap>  list=new ArrayList<>();
            for(FriendInfo friend :friendlist){
                HashMap<String,String> temp=new HashMap<>();
                temp.put("userid",friend.getFriendId()+"");
                temp.put("avatar",friend.getFriendAvatar());
                temp.put("nickname",friend.getFriendNickName());
                list.add(temp);
            }

            result.put("friendlist",list);
            return result;

        }
        catch(Exception e){
           logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }




    /**
     *  添加好友
     * @return
     */
    @ResponseBody
    @RequestMapping("addFriend")
    public Map addFriend(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();

            long userid=Long.valueOf((String)map.get("userid"));
            long friendid=Long.valueOf((String)map.get("friendid"));
            FriendInfo friend=friendService.getFriendById(userid,friendid);
            if(null!=friend){//已经是朋友了
                result.put("msg","already_friend");
                return  result;
            }
            friendService.addFriend(userid,friendid);
            friendService.addFriend(friendid,userid);
            //IMUtil.addFriend(userid+"",friendid+"");//从app直接向环信添加好友,成功后再调服务端

            result.put("result","1");


            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }



    /**
     *  删除好友
     * @return
     */
    @ResponseBody
    @RequestMapping("delFriend")
    public Map delFriend(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();

            long userid=Long.valueOf((String)map.get("userid"));
            long friendid=Long.valueOf((String)map.get("friendid"));
            FriendInfo friend=friendService.getFriendById(userid,friendid);
            if(null==friend){//不是好友关系　
                result.put("msg","not_friend");
                return result;
            }
            friendService.deleteFriend(userid,friendid);
            friendService.deleteFriend(friendid,userid);
            //IMUtil.delFriend(userid+"",friendid+"");//app先调服务端再调环信

            result.put("result","1");


            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }



    /**
     *  查找好友
     * @return
     */
    @ResponseBody
    @RequestMapping("search")
    public Map searchFriend(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();

            String tel=(String)map.get("tel");
            long userid=Long.valueOf((String)map.get("userid"));
            User user=userService.getUserByTel(tel);
            if(null==user){
                result.put("msg","not_found");
                return result;
            }
            String isFriend="0";
            String nickName=user.getNick_name();
            FriendInfo friend=friendService.getFriendById(userid,user.getId());
            if(null!=friend){
                isFriend="1";
                nickName=friend.getFriendNickName();
            }

            HashMap<String,String> hash=new HashMap();
            hash.put("userid",user.getId()+"");
            hash.put("nickname",nickName);
            hash.put("avatar",user.getAvatar());
            hash.put("sex",user.getSex());
            hash.put("birthday",user.getBirthday());
            hash.put("isFriend",isFriend);

            result.put("searchUser",hash);

            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }




    /**
     *  修改备注
     * @return
     */
    @ResponseBody
    @RequestMapping("modifyNickname")
    public Map modifyNickname(@RequestBody Map map){
        try{
            HashMap result= ReturnMap.getSuccessMap();

            String nickname=(String)map.get("nickname");
            long userid=Long.valueOf((String)map.get("userid"));
            long friendid=Long.valueOf((String)map.get("friendid"));

            FriendInfo friend =friendService.getFriendById(userid,friendid);
            if(null==friend){
                result.put("msg","not_friend");
                return result;
            }

            if(friendService.modifyFriendNickName(userid,friendid,nickname)){
                HashMap<String,String> temp=new HashMap();
                temp.put("userid",friend.getFriendId()+"");
                temp.put("avatar",friend.getFriendAvatar());
                temp.put("nickname",nickname);
                result.put("newFriend",temp);
            }else{
                result.put("msg","modify_fail");
            }





            return result;

        }catch(Exception e){
            logger.error("",e);
            return ReturnMap.exceptionMap;
        }

    }


}
